package com.yixin.hospital.controller;

import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.dto.req.patientaddress.PatientAddressAddReqDTO;
import com.yixin.hospital.entity.dto.req.patientaddress.PatientAddressEditReqDTO;
import com.yixin.hospital.service.PatientAddressService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/patient-address")
@Api(value = "患者地址管理", tags = "患者地址管理")
public class PatientAddressController {

    @Resource
    private PatientAddressService patientAddressService;

    @GetMapping("/list")
    @ApiOperation(value = "患者地址列表")
    public ApiResult<?> list() {
        return ApiResult.success(patientAddressService.list());
    }

    @PostMapping("/addOrEdit")
    @ApiOperation(value = "添加患者地址")
    public ApiResult<?> add(@Valid @RequestBody PatientAddressAddReqDTO patientAddressAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return patientAddressService.add(patientAddressAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑患者地址")
    public ApiResult<?> edit(@Valid @RequestBody PatientAddressEditReqDTO patientAddressEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return patientAddressService.edit(patientAddressEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除患者地址")
    public ApiResult<?> remove(@PathVariable int id) {
        patientAddressService.removeById(id);
        return ApiResult.success();
    }

}
